﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Bitirme.DAL.Entities;
using Bitirme.DAL.Enum;
using Bitirme.DAL.Data_Access;
using Bitirme.DAL.Utils;

namespace Bitirme.Controllers
{
    public class WorkPackageHandler
    {
        public int InsertWorkPackage(string name, string subject, string requirement, int manhour, int cost, int workState, Project project, List<WorkPackage> dependencyList){

            WorkPackage workPackage = new WorkPackage();
            PackageRequirement packageRequirement = new PackageRequirement();
            Capability newCapability = null;
            workPackage.Name = name;
            workPackage.Subject = subject;
            packageRequirement.Cost = cost;
            packageRequirement.ManHour = manhour;
            int capabilityID = -1;
            int workPackageID = -1;
            List <Capability> capabilities = CapabilityCollection.GetAllCapabilities();
            foreach (Capability cap in capabilities) {
                if (cap.Name == requirement) {
                    capabilityID = cap.ID;
                    newCapability = cap;
                    break;
                }
            }
            if (capabilityID == -1) { 
            
                newCapability = new Capability();
                newCapability.Name = requirement;
                capabilityID = CapabilityCollection.InsertCapability(newCapability);
            }

            newCapability.ID = capabilityID;
            packageRequirement.RequirementCapability = newCapability;
            workPackage.Requirement = packageRequirement;                
            switch (workState)
            {
                case 0:
                    workPackage.PackageState = State.None;
                    break;
                case 1:
                    workPackage.PackageState = State.Active;
                    break;
                case 2:
                    workPackage.PackageState = State.Pasive;
                    break;
                case 3:
                    workPackage.PackageState = State.Suspended;
                    break;
                default:
                    workPackage.PackageState = State.None;
                    break;
            }

            workPackageID = WorkPackageCollection.InsertWorkPackage(workPackage);
            workPackage.ID = workPackageID;
            if (workPackageID != -1) {
                PackageRequirementCollection.InsertPackageRequirement(workPackage);
                PackageToProjectCollection.InsertPackageToProject(workPackage, project);

                if (dependencyList.Count > 0)
                {
                    DependencyCollection.InsertDependencyToWorkPackage(workPackage, dependencyList);
                }
            }

            return 1;
        }

        public List <WorkPackage> GetAllWorkPackagesByProjectID(int projectID) {
            return PackageToProjectCollection.GetAllWorkPackagesByProjectID(projectID);
        }

        /*** Workpackagesları requirementsları ve dependencyleri ile beraber getirir ***/
        public List<WorkPackage> GetAllWorkPackagesWithRequirementsAndDependenciesByProjectID(int projectID)
        {
            List<WorkPackage> wpackages;

            /*** Workpackageları requirementlarıyla beraber getirir ***/
            wpackages = PackageToProjectCollection.GetAllWorkPackagesByProjectID(projectID);

            /*** Dependencyleri getirilecek ***/
            foreach(WorkPackage wpackage in wpackages)
            {
                List<int> dependencyIDList;
                dependencyIDList = DependencyCollection.GetDependenciesIDByWorkPackagesID(wpackage.ID);

                /*** her workpackage için bir dependency list hazırlanır ***/
                List<WorkPackage> dependencyList = new List<WorkPackage>();

                /*** workpackage için tüm dependencylerin idleri var projedeki workpackagelar ile eşlenecek (ağaç yapısı için)***/
                /*** Edit-1: Ağaç yapısı gerçekten gerekli mi??? ***/
                foreach(int id in dependencyIDList)
                {
                    /*** projedeki tüm packagelar tekrar taranarak idlere uygun nesneler bulunur ***/
                    foreach (WorkPackage dependency in wpackages)
                    {
                        /*** id ile eşleşen nesne bulunur ***/
                        if (id == dependency.ID)
                        {
                            dependencyList.Add(dependency);
                            break;
                        }
                    }
                }

                /*** dependencyler bulununca liste eşitlenir ***/
                wpackage.Dependency = dependencyList;
            }


            return wpackages;
        }

        public Boolean DeleteWorkPackageByID(int workPackageID) {

            return WorkPackageCollection.DeleteWorkPackageByID(workPackageID);
        }

        public Boolean DeletePackageToProjectByWorkPackageID(int workPackageID) {

            return PackageToProjectCollection.DeletePackagesToProjectByWorkPackageID(workPackageID);
        }

        public Boolean DeletePackageRequirementByWorkPackageID(int workPackageID) {

            return PackageRequirementCollection.DeletePackageRequirementByWorkPackageID(workPackageID);
        }

        public Boolean UpdateWorkPackageStateByID(int workPackageID, string state)
        {

            return WorkPackageCollection.UpdateWorkPackageStateByID(workPackageID, state);
        }
    }
}